//
// Created by Jeffrey Wei on 2022/11/2.
//

import Foundation
import RxSwift

public extension PrimitiveSequenceType where Trait == SingleTrait, Element == NetworkResponse {
    func mapData() -> Single<Any?> {
        map { $0.data }
    }

    func mapModel<T>(modelType: T.Type) -> Single<T?> {
        map {
            guard let data = $0.data as? [String: Any] else { return nil }
            return type(of: $0).parsingModel(type: modelType, json: data)
        }
    }

    func mapModels<T: NSObject>(modelType: T.Type) -> Single<[T]> {
        map {
            guard let data = $0.data as? [[String: Any]] else { return [] }
            return type(of: $0).parsingModelArray(type: modelType, json: data)
        }
    }
}

